﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace DHYHN.ASPNET.HoiNghi
{

    public class DataAcess
    {

        public SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["HoiNghiConnectionString"].ConnectionString);
        public SqlDataAdapter da = new SqlDataAdapter();
        public SqlCommand com = new SqlCommand();
        public DataAcess()
        {
            com.Connection = con;
        }

        public DataTable getDataTable(string sql, int start, int max,string tableName)
        {
            DataTable dt = new DataTable(tableName);
            da = new SqlDataAdapter(sql, con);
            da.Fill(start,max, dt);
            return dt;
        }

        public DataTable getDataTable(string sql)
        {
            DataTable dt = new DataTable();
            da = new SqlDataAdapter(sql, con);
            da.Fill(dt);
            return dt;
        }

        public int getCount(string sql)
        {
            DataTable dt = new DataTable();
            da = new SqlDataAdapter(sql, con);
            da.Fill(dt);
            return dt.Rows.Count;
        }

        private void OpenConnection()
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
        }

        private void CloseConnection()
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        public void ThucThiSql(string sqlCommand)
        {
            OpenConnection();
            com.CommandText = sqlCommand;
            com.ExecuteNonQuery();
            CloseConnection();
        }

        public string GetID_ByName(string sql, int columnIndex)
        {
            DataTable dt = new DataTable();
            da = new SqlDataAdapter(sql, con);
            da.Fill(dt);
            string kq = string.Empty;
            if (dt.Rows.Count > 0)
                kq = Convert.ToString( dt.Rows[0].ItemArray.GetValue(columnIndex));
            return kq;
        }

        public bool hasValue(string sql)
        {
            DataTable dt = new DataTable();
            da = new SqlDataAdapter(sql, con);
            da.Fill(dt);
            
            return dt.Rows.Count>0;
        }


    }

}
